Logical OR Operator ( || )
The C++ logical OR operator ( || ) is a binary operator that returns true if at least one of its operands is true. It returns false only when both operands are false. Here’s the truth table for the OR operator:
Operand 1 |
Operand 2 |
Result |
---|---|---|
true |
true |
true |
true |
false |
true |
false |
true |
true |
false |
false |
false |
Syntax of Logical OR
expression1 || expression2
Example of Logical OR in C++
C++
// C++ program to demonstrate the logical or operator #include <iostream> using namespace std; int main() { int num = 7; // using logical or for conditional statement if (num <= 0 || num >= 10) { cout << "The number is outside the range of 0 to 10." << endl; } else { cout << "The number is between 0 to 10." << endl; } return 0; } |
The number is between 0 to 10.
Explanation: In the above code, the condition num < 0 || num > 10 checks whether the number is either less than equal to 0 or greater than equal to 10. If either of these conditions is true, the message “The number is outside the range of 0 to 10.” will be printed otherwise else statement is printed.
C++ Logical Operators
In C++ programming languages, logical operators are symbols that allow you to combine or modify conditions to make logical evaluations. They are used to perform logical operations on boolean values (true or false).
In C++, there are three logical operators:
- Logical AND ( && ) Operator
- Logical OR ( || ) Operator
- Logical NOT ( ! ) Operator
Let’s discuss each of the operators in detail.
Contact Us